#!/bin/bash
DockerImages(){
    tmp=`docker images |grep k8s-admin`
    if [[ -n $(docker images |grep k8s-admin | awk '{print $1}') ]];then
		docker_name=`docker images |grep k8s-admin | awk '{print $1}'`
		docker_tag=`docker images |grep k8s-admin | awk '{print $2}'`
		tmp=`docker rmi $docker_name":"$docker_tag`
		echo "delete docker images: ${tmp}......"
		echo "create docker images......"
		docker_img=`docker build -t k8s-admin:latest .`
		dicker_id=`docker run -it -d --name=k8s -p 8899:8899 k8s-admin:latest`

    else
		echo "not docker images......"
        echo "create docker images......"
        docker_img=`docker build -t k8s-admin:latest .`
		echo "start docker container......"
        dicker_id=`docker run -it -d --name=k8s -p 8899:8899 k8s-admin:latest`
    fi
}

# 查看镜像是否有运行的容器
DOCKER_CONTAINER_ID=`docker ps -a |grep k8s | awk '{print $1}'`
if [[ -n $(docker ps -a |grep k8s |awk '{print $1}') ]];then
    echo "$DOCKER_CONTAINER_ID，start......"
    echo "close $DOCKER_CONTAINER_ID......"
    is_ok=`docker stop $DOCKER_CONTAINER_ID`
    echo $is_ok
    if [ $is_ok ];then
        docker_tmp=`docker rm ${DOCKER_CONTAINER_ID}`
        echo $docker_tmp
        echo "docker container close ok and docker container delete ok......"
        DockerImages
    fi
else
    echo "not docker container"
    DockerImages
fi